package com.kashow;

import android.content.Context;
import android.net.Uri;
import android.support.multidex.MultiDexApplication;
import android.text.TextUtils;
import android.view.Gravity;
import android.widget.Toast;

import com.github.mzule.activityrouter.router.RouterCallback;
import com.github.mzule.activityrouter.router.RouterCallbackProvider;
import com.github.mzule.activityrouter.router.SimpleRouterCallback;
import com.kashow.base.Ext;
import com.kashow.base.network.Network;
import com.kashow.base.utils.ViewUtils;
import com.squareup.leakcanary.LeakCanary;
import com.zhy.autolayout.config.AutoLayoutConifg;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class AppContext extends MultiDexApplication implements RouterCallbackProvider {
    private static AppContext mAppContext;
    private static CharSequence lastToast = "";
    private static long lastToastTime;
    private com.kashow.injector.component.ApplicationComponent mApplicationComponent;
    private boolean hasLogin;
    public static final String APP_ID = "8f7335df04"; // TODO 替换成bugly上注册的appid

    private boolean mIsDebug = BuildConfig.BuildType < 3;
    private int mUid;

    @Override
    public void onCreate() {
        super.onCreate();
        mAppContext = this;
        if (LeakCanary.isInAnalyzerProcess(this)) {
            return;
        }
        mApplicationComponent = com.kashow.injector.component.DaggerApplicationComponent.builder()
                .applicationModule(new com.kashow.injector.module.ApplicationModule(this))
                .build();
        mApplicationComponent.inject(this);
        LeakCanary.install(this);

        AutoLayoutConifg.getInstance().useDeviceSize();
        //初始化友盟推送

    }



    /**
     * 获取进程号对应的进程名
     *
     * @param pid 进程号
     * @return 进程名
     */
    private static String getProcessName(int pid) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader("/proc/" + pid + "/cmdline"));
            String processName = reader.readLine();
            if (!TextUtils.isEmpty(processName)) {
                processName = processName.trim();
            }
            return processName;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }


        return null;
    }

    @Override
    public RouterCallback provideRouterCallback() {
        return new SimpleRouterCallback() {
            @Override
            public void beforeOpen(Context context, Uri uri) {
                //context.startActivity(new Intent(context, com.kashow.ui.main.MainActivity.class));
            }

            @Override
            public void afterOpen(Context context, Uri uri) {
                //context.startActivity(new Intent(context, LoginActivity.class));
            }

            @Override
            public void notFound(Context context, Uri uri) {
                //context.startActivity(new Intent(context, com.kashow.ui.NotFoundActivity.class));
            }

            @Override
            public void error(Context context, Uri uri, Throwable e) {
                //context.startActivity(com.kashow.ui.ErrorStackActivity.makeIntent(context, uri, e));
            }
        };
    }

    public static AppContext getApplication() {
        return mAppContext;
    }

    public static Context context() {
        return getApplication().getApplicationContext();
    }


    public static void showToastLong(int message) {
        showToast(message, Toast.LENGTH_LONG, 0);
    }

    public static void showToastLong(String message) {
        showToast(message, Toast.LENGTH_LONG, 0, Gravity.BOTTOM);
    }

    public static void showToastLong(CharSequence message) {
        showToast(message, Toast.LENGTH_LONG, 0, Gravity.BOTTOM);
    }

    public static void showToastLong(int message, int icon) {
        showToast(message, Toast.LENGTH_LONG, icon);
    }

    public static void showToastLong(String message, int icon) {
        showToast(message, Toast.LENGTH_LONG, icon, Gravity.BOTTOM);
    }

    public static void showToast(int message) {
        showToast(message, Toast.LENGTH_SHORT, 0);
    }

    public static void showToast(String message) {
        showToast(message, Toast.LENGTH_SHORT, 0, Gravity.BOTTOM);
    }

    public static void showToast(CharSequence message) {
        showToast(message, Toast.LENGTH_SHORT, 0, Gravity.BOTTOM);
    }


    public static void showToast(int message, Object... args) {
        showToast(message, Toast.LENGTH_SHORT, 0, Gravity.BOTTOM, args);
    }

    public static void showToast(int message, int duration, int icon) {
        showToast(message, duration, icon, Gravity.BOTTOM);
    }

    public static void showToast(int message, int duration, int icon,
                                 int gravity) {
        showToast(context().getString(message), duration, icon, gravity);
    }

    public static void showToast(int message, int duration, int icon,
                                 int gravity, Object... args) {
        showToast(context().getString(message, args), duration, icon, gravity);
    }


    public static final class Utils extends Ext {

        @Override
        public String getCurOpenId() {
            return null;
        }

        @Override
        public String getDeviceInfo() {
            return null;
        }

        @Override
        public String getPackageNameForResource() {
            return "com.kashow.demo";
        }

        @Override
        public int getScreenHeight() {
            return ViewUtils.getScreenHeight();
        }

        @Override
        public int getScreenWidth() {
            return ViewUtils.getScreenWidth();
        }

        @Override
        public boolean isAvailable() {
            return Network.isAvailable();
        }

        @Override
        public boolean isWap() {
            return Network.isWap();
        }

        @Override
        public boolean isMobile() {
            return Network.isMobile();
        }

        @Override
        public boolean is2G() {
            return Network.is2G();
        }

        @Override
        public boolean is3G() {
            return Network.is3G();
        }

        @Override
        public boolean isWifi() {
            return Network.isWifi();
        }

        @Override
        public boolean isEthernet() {
            return Network.isEthernet();
        }
    }

    public static void showToast(CharSequence message, int duration, int icon,
                                 int gravity) {
//        if (!TextUtils.isEmpty(message)) {
//            long time = System.currentTimeMillis();
//            if (!message.toString().equals(lastToast.toString())
//                    || Math.abs(time - lastToastTime) > 2000) {
//                View view = LayoutInflater.from(context()).inflate(
//                        R.layout.view_toast, null);
//                ((TextView) view.findViewById(R.id.title_tv)).setText(message);
//                if (icon != 0) {
//                    ((ImageView) view.findViewById(R.id.icon_iv))
//                            .setImageResource(icon);
//                    view.findViewById(R.id.icon_iv)
//                            .setVisibility(View.VISIBLE);
//                }
//                Toast toast = new Toast(context());
//                toast.setView(view);
//                if (gravity == Gravity.CENTER) {
//                    toast.setGravity(gravity, 0, 0);
//                } else {
//                    toast.setGravity(gravity, 0, 35);
//                }
//
//                toast.setDuration(duration);
//                toast.show();
//                lastToast = message;
//                lastToastTime = System.currentTimeMillis();
//            }
//        }
    }

}
